﻿using System;
using System.Collections.Generic;
using System.Text;

namespace PlugNT.Database
{

    public class LeftJoinInfo
    {
        private LeftJoinInfo _joininfo = null;

        private string _andWhere = "";

        private string _asName="tmp";

        private string _colNames = null;

        private string _tableName = null;    

        private string _onTargetColumn = null;

        private string _onColumn = null;



        public LeftJoinInfo(string tableName, string colNames, string onTargetColumn,string onColumn,string andWhere)
        {
            this._tableName = tableName;
            this._colNames = colNames;
            this._onTargetColumn = onTargetColumn;
            this._onColumn = onColumn;
            this._andWhere = andWhere;
        }

        public LeftJoinInfo(string tableName, string colNames, string onTargetColumn,string onColumn, string andWhere , LeftJoinInfo joininfo)
        {
            this._tableName = tableName;
            this._colNames = colNames;
            this._onTargetColumn = onTargetColumn;
            this._onColumn = onColumn;
            this._joininfo = joininfo;
            this._andWhere = andWhere;

            if(Exists(tableName))
            {
                throw new Exception("已连接相同的tableName！");
            }
        }



        private int asFlag = 1;
        private bool Exists(string tableName)
        {
            if (_joininfo != null)
            {
                if (_tableName != _joininfo._tableName)
                {
                    asFlag++;
                    _joininfo.SetAsName("tmp" + asFlag);
                    return _joininfo.Exists(tableName);
                }
                return true;
            }
            return false;
        }
        private void SetAsName(string asName)
        {
            this._asName = asName;
        }


        internal string ToJoinTableString(string tName)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(" left join " + _tableName + " as " + _asName + " on " + (tName == "" ? "a" : tName) + "." + _onTargetColumn + "=" + _asName + "." + _onColumn);
            if (_joininfo != null)
            {
                sb.Append(_joininfo.ToJoinTableString(_asName));
            }
            return sb.ToString();
        }

        internal string ToJoinColumnsString()
        {
            StringBuilder sb = new StringBuilder();
            if (_joininfo != null)
            {
                sb.Append(_joininfo.ToJoinColumnsString());
            }
            if (sb.ToString() != "")
            {
                sb.Append(",");
            }

            string[] tmpColumns = _colNames.Split(',');
            bool firFlag = true;
            foreach (string s in tmpColumns)
            {
                if (firFlag)
                {
                    firFlag = false;
                    sb.Append(_asName + "." + s);
                }
                else
                {
                    sb.Append("," + _asName + "." + s);
                }
            }
            return sb.ToString();
        }

        internal string ToJoinAndWhereString()
        {
            StringBuilder sb = new StringBuilder();
            if (_joininfo != null)
            {
                sb.Append(_joininfo.ToJoinAndWhereString());
            }
            if (_andWhere != "")
            {
                sb.Append(" " + _andWhere).Replace(" and ", " and " + _asName + ".");
            }
            return sb.ToString();
        }

    }
}
